package com.ptsolusi.ceriacrudwicket.tool.pages.windows;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import com.ptsolusi.ceriacrudwicket.logic.CustomerLogic;
import com.ptsolusi.ceriacrudwicket.model.Customer;
import com.ptsolusi.ceriacrudwicket.tool.Locator;
import com.ptsolusi.ceriacrudwicket.tool.constant.OperationType;

public class CustomerPanel extends Panel {
	private static final long serialVersionUID = 1L;
	
	//default to create and update
	private String lblBtnYes = "button.save";
	private String lblBtnNo = "button.cancel";
	
	public CustomerPanel(String id, final ModalWindow window, final Customer customer, final OperationType type) {
		super(id);
		
		window.setInitialHeight(150);
		window.setInitialWidth(350);
		
		
		
		if(type.equals(OperationType.DELETE)){
			lblBtnYes = "button.yes";
			lblBtnNo = "button.no";
		} else if(type.equals(OperationType.READ)){
			lblBtnNo = "button.ok";
		}
		
		
		Form form = new Form("form");
		
		Label lblDeleteConfirm = new Label("lblDeleteConfirm", "Are you want to delete this customer ?");
		form.add(lblDeleteConfirm);
		if(!type.equals(OperationType.DELETE))	 //visibility
			lblDeleteConfirm.setVisible(false);
		
		
		TextField txtFirstName = new TextField("txtFirstName", new PropertyModel(customer, "firstName"));
		TextField txtLastName = new TextField("txtLastName", new PropertyModel(customer, "lastName"));
		
		
		if(type.equals(OperationType.DELETE) || type.equals(OperationType.READ)){
			txtFirstName.setEnabled(false);
			txtLastName.setEnabled(false);
		}
		
		
		form.add(txtFirstName);
		form.add(txtLastName);
		
		
		@SuppressWarnings("serial")
		AjaxFallbackButton btnYes = new AjaxFallbackButton("btnYes", new ResourceModel(lblBtnYes), form) {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form form) {
				if(type.equals(OperationType.CREATE) || type.equals(OperationType.UPDATE))
					saveOrUpdateCustomer(customer);
				else if(type.equals(OperationType.DELETE))
					deleteCustomer(customer);
				window.close(target);
			}
		};
		if(type.equals(OperationType.READ))
			btnYes.setVisible(false);
		form.add(btnYes);
		
		
		@SuppressWarnings("serial")
		AjaxFallbackButton btnNo = new AjaxFallbackButton("btnNo", new ResourceModel(lblBtnNo), form) {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form form) {
				window.close(target);
			}
		};
		form.add(btnNo);
		
		add(form);
		
	}
	
	private void saveOrUpdateCustomer(Customer customer){
		getCustomerLogic().saveOrUpdateCustomer(customer);
	}
	
	private void deleteCustomer(Customer customer){
		getCustomerLogic().deleteCustomer(customer);
	}
	
	
	protected CustomerLogic getCustomerLogic(){
		return Locator.getCustomerLogic();
	}
}
